package cn.dnf.pet.service.impl;

import cn.dnf.basic.exception.BusinessException;
import cn.dnf.basic.service.IBaiduAiAuditService;
import cn.dnf.basic.service.impl.BaseServiceImpl;
import cn.dnf.basic.util.*;
import cn.dnf.order.domain.OrderPetAcquisition;
import cn.dnf.order.mapper.OrderPetAcquisitionMapper;
import cn.dnf.org.domain.Employee;
import cn.dnf.org.domain.Shop;
import cn.dnf.org.mapper.EmployeeMapper;
import cn.dnf.org.mapper.ShopMapper;
import cn.dnf.pet.domain.Pet;
import cn.dnf.pet.domain.SearchMasterMsg;
import cn.dnf.pet.domain.SearchMasterMsgAuditLog;
import cn.dnf.pet.dto.AcceptDto;
import cn.dnf.pet.mapper.PetMapper;
import cn.dnf.pet.mapper.SearchMasterMsgAuditLogMapper;
import cn.dnf.pet.mapper.SearchMasterMsgMapper;
import cn.dnf.pet.query.SearchMasterMsgQuery;
import cn.dnf.pet.service.ISearchMasterMsgService;
import cn.dnf.user.domain.User;
import cn.dnf.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class SearchMasterMsgServiceImpl extends BaseServiceImpl<SearchMasterMsg> implements ISearchMasterMsgService {
    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private IBaiduAiAuditService baiduAiAuditService;
    @Autowired
    private SearchMasterMsgAuditLogMapper searchMasterMsgAuditLogMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private PetMapper petMapper;
    @Autowired
    private OrderPetAcquisitionMapper orderPetAcquisitionMapper;

    @Override
    public void publish(SearchMasterMsg smm, Long id) {
        //1.先添加到数据库t_serchMasterMsg state:0 待审核
        //user_id shop_id
        //通过logininfoid查询userid
        User user = userMapper.loadByLogininfoId(id);
        smm.setUser_id(user.getId());
        smm.setState(0);
        smm.setNote("待审核");

        //获取当前地址
        Point point = DistanceUtils.getPoint(smm.getAddress());
        Shop nearestShop = DistanceUtils.getNearestShop(point,shopMapper.loadAll());
        smm.setShop_id(nearestShop.getId());

        searchMasterMsgMapper.save(smm);
        //2.审核
        //审核文本
        Boolean testBoolean = baiduAiAuditService.textAudit(smm.getName() + smm.getAddress() + smm.getCoat_color());

        //审核图片
        Boolean imgBoolean = baiduAiAuditService.imageAudit(smm.getResources());
        if(testBoolean && imgBoolean){
            //审核通过
            smm.setState(1);
            smm.setNote("待处理");
            searchMasterMsgMapper.update(smm);

            //2.添加审核日志
            SearchMasterMsgAuditLog smmal = new SearchMasterMsgAuditLog();
            smmal.setMsg_id(smm.getId());
            smmal.setState(1);
            smmal.setAudit_time(new Date());
            smmal.setNote("待处理状态!");
            //保存审核日志信息
            searchMasterMsgAuditLogMapper.save(smmal);
            //短信,邮箱

        }else{//审核失败
            //1.修改状态 note
            smm.setNote("驳回,信息异常");

            SearchMasterMsgAuditLog smmal = new SearchMasterMsgAuditLog();
            smmal.setMsg_id(smm.getId());
            smmal.setState(0);
            smmal.setAudit_time(new Date());
            smmal.setNote("审核不通过,驳回,信息异常!");
            searchMasterMsgAuditLogMapper.save(smmal);
            throw new BusinessException("信息异常,请重新录入");



        }



    }

    @Override
    public PageList<SearchMasterMsg> toHandle(SearchMasterMsgQuery query, Long id) {
        //通过logininfoid查询employee
        Employee emp = employeeMapper.loadByLogininfoId(id);
        //待处理状态
        query.setState(1);
        if(emp.getShop_id()!=null){
            query.setShopId(emp.getShop_id());
        }

        return  super.pageQuery(query);
    }

    @Override
    public PageList<SearchMasterMsg> user(SearchMasterMsgQuery query, Long id) {
        //查询用户id
        User user = userMapper.loadByLogininfoId(id);
        query.setUserId(user.getId()); //<if userId!=null> and user_id=#{user_id}<>

        return super.pageQuery(query);
    }

    @Override
    public void reject(Long id) {

        searchMasterMsgMapper.reject(id);
    }

    @Override
    public void accept(AcceptDto dto, Long id) {
        Long empId = dto.getHandler();
        Long msgId = dto.getMsgId();
        String note = dto.getNote();
        //根据ssmid查询订单,如果查得到,该订单正在处理中
        OrderPetAcquisition order = orderPetAcquisitionMapper.loadBySmmId(msgId);
        if(order!=null){//之前接单了
            throw new BusinessException("订单正在处理中...");

        }

        //1.生成一个pet :根据searchMasterMsg生成.
        SearchMasterMsg searchMasterMsg = searchMasterMsgMapper.loadById(msgId);
        Pet pet = searchMasterMsg2Pet(searchMasterMsg);
        //保存
        petMapper.save(pet);
        //2.生成一个收购订单
        OrderPetAcquisition opa = ininOrderPetAcquisition(dto,searchMasterMsg,pet);
        //3.保存订单
        orderPetAcquisitionMapper.save(opa);
    }

    /**
     * 生成一个订单对象
     * @param dto
     * @param pet
     * @return
     */
    private OrderPetAcquisition ininOrderPetAcquisition(AcceptDto dto, SearchMasterMsg smm, Pet pet) {

        //创建一个订单对象,赋值
        OrderPetAcquisition order = new OrderPetAcquisition();

        //随机生成一个
        order.setOrderSn(CodeGenerateUtils.generateOrderSn(smm.getUser_id()));
        order.setDigest("["+smm.getTitle()+"]");
        //最后确认时间

        order.setLastcomfirmtime(new Date(System.currentTimeMillis()+2*24*60*1000));
        order.setState(0);//待处理
        order.setPrice(smm.getPrice());
        order.setPet_id(pet.getId());
        order.setUser_id(smm.getUser_id());
        order.setShop_id(smm.getShop_id());
        order.setAddress(smm.getAddress());
        order.setEmp_id(dto.getHandler());
        order.setSearch_master_msg_id(dto.getMsgId());




        return order;
    }

    /**
     * 通过searchMasterMsg生成一个pet
     * @return
     */
    private Pet searchMasterMsg2Pet(SearchMasterMsg smm) {
        Pet pet = new Pet();
                pet.setName(smm.getName());
                pet.setCostprice(smm.getPrice());
                pet.setType_id(smm.getPet_type());
                pet.setResources(smm.getResources());
                pet.setShop_id(smm.getShop_id());
                pet.setSearch_master_msg_id(smm.getId());

        return pet;
    }


}
